package com.yin.tvolatile;


import java.util.concurrent.atomic.AtomicInteger;

/*
不保证原子性
 */
public class Vdemo02 {
    // 原子类的int
    private volatile static AtomicInteger num = new AtomicInteger();

    public static void add() {
        num.getAndIncrement();  // CAS
    }

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            new Thread(()-> {
                for (int j = 0; j < 1000; j++) {
                    add();
                }
            }).start();
        }

        while (Thread.activeCount() > 2) {
            // main  gc 线程
            Thread.yield();
        }

        System.out.println(Thread.currentThread().getName() + "  "+ num);
    }
}
